<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学期" prop="semester">
        <el-select v-model="queryParams.semester" placeholder="请选择学期" clearable>
          <el-option
            v-for="dict in dict.type.jy_semester"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学科" prop="subject">
        <el-select v-model="queryParams.subject" placeholder="请选择学科" clearable>
          <el-option
            v-for="dict in dict.type.jy_subject"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-select v-model="queryParams.grade" placeholder="请选择年级" clearable>
          <el-option
            v-for="dict in dict.type.jy_grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="名称" prop="name">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.name"-->
      <!--          placeholder="请输入名称"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="停用状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择停用状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['research:jyResearchGroup:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="jyResearchGroupList" stripe border @selection-change="handleSelectionChange">
      <el-table-column label="序号" type="index" align="center" width="50"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="学期" align="center" prop="semester">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jy_semester" :value="scope.row.semester"/>
        </template>
      </el-table-column>
      <el-table-column label="学科" align="center" prop="subject">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jy_subject" :value="scope.row.subject"/>
        </template>
      </el-table-column>
      <el-table-column label="年级" align="center" prop="grade">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jy_grade" :value="scope.row.grade"/>
        </template>
      </el-table-column>

      <el-table-column label="停用状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="参与人员" align="center" prop="teacherNames" show-tooltip-when-overflow/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['research:jyResearchGroup:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['research:jyResearchGroup:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教研组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-press-escape="false" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="学期" prop="semester">
              <el-select v-model="form.semester" placeholder="请选择学期" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.jy_semester"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学科" prop="subject">
              <el-select v-model="form.subject" placeholder="请选择学科" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.jy_subject"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年级" prop="grade">
              <el-select v-model="form.grade" placeholder="请选择年级" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.jy_grade"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="停用状态" prop="state">
              <el-select v-model="form.state" placeholder="请选择停用" style="width: 100%">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参与人员" prop="personNames">
              <el-input v-model="form.teacherNames" type="textarea" readonly placeholder="请选择参与人员" @focus="$refs.jyTeacherSelect.show()"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <JyTeacherSelect ref="jyTeacherSelect" multiple required @selection-change="handleTeacherSelectChange"></JyTeacherSelect>
  </div>
</template>

<script>
  import {addJyResearchGroup, delJyResearchGroup, getJyResearchGroup, listJyResearchGroup, updateJyResearchGroup} from "@/api/research/jyResearchGroup"
  import JyTeacherSelect from "@/views/research/jyTeacher/JyTeacherSelect.vue"

  export default {
    name: "JyResearchGroup",
    dicts: ['jy_grade', 'jy_semester', 'sys_yes_no', 'jy_subject'],
    components: {JyTeacherSelect},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 教研组表格数据
        jyResearchGroupList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          semester: null,
          subject: null,
          grade: null,
          name: null,
          state: null,
          startDate: null,
          endDate: null,
          deptId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          semester: [
            {required: true, message: "学期不能为空", trigger: "change"}
          ],
          subject: [
            {required: true, message: "学科不能为空", trigger: "change"}
          ],
          grade: [
            {required: true, message: "年级不能为空", trigger: "change"}
          ],
          state: [
            {required: true, message: "停用状态不能为空", trigger: "change"}
          ],
          deptId: [
            {required: true, message: "部门Id不能为空", trigger: "blur"}
          ],
          delFlag: [
            {required: true, message: "删除标志不能为空", trigger: "blur"}
          ],
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询教研组列表 */
      getList() {
        this.loading = true
        listJyResearchGroup(this.queryParams).then(response => {
          this.jyResearchGroupList = response.rows
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          semester: null,
          subject: null,
          grade: null,
          name: null,
          state: 'N',
          startDate: null,
          endDate: null,
          deptId: null,
          delFlag: '0',
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          teacherNames: null,
          teacherList: []
        }
        this.resetForm("form")
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = "添加教研组"
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        const id = row.id || this.ids
        getJyResearchGroup(id).then(response => {
          this.form = response.data
          this.open = true
          this.title = "修改教研组"
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.$modal.confirm('保存后数据不可更改，是否确认进行保存？')
              .then(response => {
                this.$modal.loading()
                return this.form.id != null ? updateJyResearchGroup(this.form) : addJyResearchGroup(this.form)
              })
              .then(response => {
                this.$modal.closeLoading()
                this.$modal.msgSuccess("保存成功")
                this.open = false
                this.getList()
              }).catch(err => {
              this.$modal.closeLoading()
            });
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$modal.confirm('是否确认删除教研组编号为"' + ids + '"的数据项？').then(function () {
          return delJyResearchGroup(ids)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('research/jyResearchGroup/export', {
          ...this.queryParams
        }, `jyResearchGroup_${new Date().getTime()}.xlsx`)
      },
      handleTeacherSelectChange(select) {//教师选择框发生盖板
        this.form.teacherNames = select.map(item => item.name).join()
        this.form.teacherList = select.map(item => {
          return {
            teacherId: item.id,
            teacherName: item.name
          }
        })
      }
    }
  }
</script>
